"""
	The main file that fires HeaDSET up. It also parses commandline arguments,
	and display a fancy chooser window. 
	
	@author: J.Teeuw, J.H. Donker
"""

import wx, sys

from gui import *
from model import *
from peripherals import *
from examples import *
from util import *

# NOTE: global variables are not destructed at end of function calls!!
m = None
v = None
o = None
c = None
r = None
a = None
t = None
choser_frame = None


# event callback functions
def StartModelingView(event):
	'''
		Start the modeling tool
		
		@param event: button click event
	'''
	# import globals to local variables
	global m, v, o, c, r, a
	
	# start modeling view
	m = HeaDSETModel()
	v = ModelingView(m)
	o = OutputJournal("journal.log")
	
	#Remove comments to enable examples
	#r = SimpleLogger()
	#a = AdvancedLogger()
		
	c = ModelingController(m, v)
	
	choser_frame.Close()
	

def StartOperationView(event):
	'''
		Start the operation tool
		
		@param event: button click event
	'''
	# import globals to local variables
	global m, v, o, c, r, t
	
	# start operation view
	o = OutputJournal("journal.log")

	m = HeaDSETModel()
	m.NonModelMode()
	v = OperationView(m)
	c = OperationController(m, v)
	#example
	#t = Forwarder(InputWrapper(m))
	#t.Start()

	#r = SimpleJoystickController(InputWrapper(m))
	#r.Start()
	choser_frame.Close()
	
	
def StartPlayerView(event):
	'''
		Start the player tool
		
		@param event: button click event
	'''
	# import globals to local variables
	global m, v, o, c
	
	# start player view
	o = OutputJournal("journal.log")

	m = HeaDSETModel()
	m.NonModelMode()

	v = PlayerView(m)
	c = PlayerController(m, v)
	
	choser_frame.Close()
	

def Close(event):
	'''
		Close the choser frame dialog
		
		@param event: button click event
	'''
	global choser_frame
	choser_frame.Close()


class StartFrame(wx.Frame):
        
		# Constructor
		def __init__(self, parent, id, title):
			'''
				Construct a new choser frame dialog where the user can
				chose the tool (s)he wants to use.
		
				@param parent: the parent to the choser frame
				@param id: the id of the choser frame
				@param title: the title of the dialog
			'''
			wx.Frame.__init__(self, parent, id, title, size = (250, 400),  style = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
			self.SetIcon(wx.Icon("res/HeaDSETLogo.png", wx.BITMAP_TYPE_PNG))
			
			mainPanel = wx.Panel(self)
			mainSizer = wx.BoxSizer(wx.VERTICAL)
			mainPanel.SetSizer(mainSizer)
			
			# add title
			textSizer = wx.BoxSizer(wx.VERTICAL)
			font1 = wx.Font(15, wx.SWISS, wx.NORMAL, wx.NORMAL, False)
			title = wx.StaticText(mainPanel, -1, "Choose your tool")
			title.SetFont(font1)
			textSizer.Add(title)
			mainSizer.Add(textSizer, 0, wx.ALIGN_CENTER | wx.ALL, 10)
			
			# add options buttons
			img1 = wx.Image("res/PathCreator.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
			img2 = wx.Image("res/PathRecorder.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
			img3 = wx.Image("res/PathPlayer.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
			
			gridsizer = wx.GridSizer(3, 2, 5, 5)
			
			btn_modeling = wx.BitmapButton(mainPanel, id = 101, bitmap = img1, size = (75, 75)) 
			txt_modeling = wx.StaticText(mainPanel, -1, " PathCreator\n (Modelling)")
			
			gridsizer.Add(btn_modeling, 0, wx.ALIGN_LEFT | wx.ALL, 10)
			gridsizer.Add(txt_modeling, -1, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
			
			btn_operation = wx.BitmapButton(mainPanel, id = 102, bitmap = img2, size = (75, 75))
			txt_operation = wx.StaticText(mainPanel, -1, " PathRecorder\n (Operation)")
			
			gridsizer.Add(btn_operation, 0, wx.ALIGN_LEFT | wx.ALL, 10)
			gridsizer.Add(txt_operation, -1, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

			btn_player = wx.BitmapButton(mainPanel, id = 103, bitmap = img3, size = (75, 75))
			txt_player = wx.StaticText(mainPanel, -1,  " PathPlayer\n (Playback)")
			
			gridsizer.Add(btn_player, 0, wx.ALIGN_LEFT | wx.ALL, 10)
			gridsizer.Add(txt_player, -1, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
			
			mainSizer.Add(gridsizer, 0)
			mainSizer.AddStretchSpacer()
			
			btn_exit = wx.Button(mainPanel, 104, "Exit")
			mainSizer.Add(btn_exit, 0, wx.ALIGN_RIGHT | wx.ALL, 10)
			
			# bind event
			self.Bind(wx.EVT_BUTTON, StartModelingView, id = 101)
			self.Bind(wx.EVT_BUTTON, StartOperationView, id = 102)
			self.Bind(wx.EVT_BUTTON, StartPlayerView, id = 103)
			self.Bind(wx.EVT_BUTTON, Close, id = 104)
			
			# show frame
			self.Centre()
			self.Show()
			
			# RSLV: intialize common components?

# main function

def main():
	'''
		Entry point for the main application
		Parses command line arguments and show the choser frame dialog
	'''
	global choser_frame
	debug = False
	model = False
	operation = False
	player = False
	args = False
	snake = False
	# start application
	for arg in sys.argv:
		if arg == "--Development":
			debug = True

		elif arg == "--Snake":

			snake = True
		elif arg == "--Model":
			model = True
			operation = False
			player = False
		elif arg == "--Operation":
			model = False
			operation = True
			player = False	
		elif arg == "--Player":
			model = False
			operation = False
			player = True
		elif arg == "--Args":
			args = True
			debug = True
		elif arg == "Main.py":
			pass
		else:
			print "Unkown argument:", arg
			args = True
			debug = True
	
	if snake:
		s = Snake()
	
	if not debug:
		app = wx.App()
		out = open("stdout.hlf","a")
		err = open("stderr.hlf", "a")
		
		sys.stdout = out
		sys.sterr = err		
	else:
		print "debug"
		app = wx.PySimpleApp()

	choser_frame = StartFrame(None, -1, "Welcome to HeaDSET")
	
	if args:
		print "Twente University HeaDSET  (Beta) arguments"
		print "--Development, runs HeaDSET with full debug information"
		print "--Model, run the model application"
		print "--Operation, run the operation application"
		print "--Player, run the the Player application"
		print "--Args, display the argument list"
	elif model:
		StartModelingView(None)
	elif operation:
		StartOperationView(None)
	elif player:
		StartPlayerView(None)
	
	# start application loop
	app.MainLoop()
	if snake:
		s.PrintStats()

if __name__ == "__main__":
	main()
